<?php

abstract class UngvienTiisController extends Controller {

    /**
     * @desc layout
     */
    public $layout;

    /**
     * @desc auth ungvien
     */
    public $_auth;
    /**
     * @desc id ungvien
     */
    public $_uinfo;

    /**
     * @desc menus sysadmin 
     */
    public $_menus;

    /**
     * @desc top nav sysadmin 
     */
    public $_topnav;

    public function init() {
        parent::init();
        
        // Load TIIS Bootstrap.
        Yii::app()->setAliases(array('bootstrap' => 'ext.bootstrap'));
        
        
        //set Theme & layout
        $this->layout = '//layouts/main';
        $theme = 'default';
        
        Yii::app()->setTheme(file_exists(dirname(__FILE__) . '/../../themes/' . $theme) ? $theme : 'default');
        
    }

    public function beforeAction($action) {
        
         if ($action->id == 'logout')
             $this->actionLogout ();
        
        //check authentication
        $this->_auth = (Yii::app()->user->getState('info_ungvien')->role == UngvienIdentity::UNGVIEN_ROLE ? true : false);
        if ($action->id != 'login') {
            if (!$this->_auth)//=> yet not login
                $this->redirect(Yii::app()->baseUrl . '/ungvien/login.html');
        }elseif ($this->_auth && $action->id == 'login') {
            $this->redirect(Yii::app()->baseUrl . '/ungvien/mykiemviec.html');
        }
        
        return true;
    }

    /**
     * @desc login sysadmin 
     */
    public function actionLogin() {
        //set the layout
        $this->layout = '//layouts/ungvien_login';
        
        $this->setPageTitle('Ứng viên - Đăng nhập');
        $ungvienForm = new UngvienLoginForm();
        if (isset($_POST['UngvienLoginForm'])) {
            $ungvienForm->attributes = $_POST['UngvienLoginForm'];
            if ($ungvienForm->validate() && $ungvienForm->login()) {
                $this->redirect(Yii::app()->baseUrl . '/ungvien/mykiemviec.html');
            }
        }
        $this->render('login', array('ungvienForm' => $ungvienForm));
    }

    /**
     * @desc logout sysadmin 
     */
    public function actionLogout() {
       
        Yii::app()->user->setState('info_ungvien', false);
        $this->redirect(Yii::app()->getBaseUrl(true));
    }

}
